<?php
namespace onlineChat\server\http;
use onlineChat\model\Session;
use onlineChat\model\Message;
use onlineChat\model\Publisher;
use onlineChat\model\Customer;

class CustomerApi{ 
    /**
     * 获取咨询客服的用户
     */
    static function getCustomerContacts($get){
        return httpApiMsg(200,'',Customer::$waitQueues);
    }
    /**
     * 客服接入
     */
    static function customerJoin($get){
        
        if( empty($get['uid']) ){
            return httpApiMsg(100,'uid参数不能为空！');
        }
        if( empty($get['to_id']) ){
            return httpApiMsg(100,'to_id参数不能为空！');
        }
        if( !isset($get['tmp']) ){
            $get['tmp'] = 1;
        }
         
        $sessions = Session::getByUid($get['to_id'],$get['tmp']);
        //var_export($sessions);
        foreach( $sessions as $session ){
            if( $session['to_id'] != "" ){
                return httpApiMsg(100,'已接入客服了！');
            }
        }
        if( empty($sessions) ){
            return httpApiMsg(100,'用户不存在！');
        }
        $msg = Message::genCustomerJoin($get['uid'],$get['to_id']);
        Publisher::instance()->publish($msg);
        foreach( $sessions as $session ){
            $access_token = $session['access_token'];
            Session::writeFrameByAccessToken($access_token,$msg);
            Session::$sessions[$access_token]['to_id'] = $get['uid'];
            Session::$sessions[$access_token]['customer_id'] = $get['uid'];
            //var_dump(Session::$sessions[$access_token],$access_token);
        }
        if( !empty($session) ){
            //给客服广播用户已接入
            $msg = Message::genCustomerJoin($session['uid']);
            //var_export($msg);
            foreach( Session::$customerIndex as $access_token=>$v ){
                Session::writeFrameByUid(Session::$sessions[$access_token]['uid'],$msg);
            }
        }
        Customer::delWaitQueue($get['to_id']);
        return httpApiMsg(200,'操作成功！');
        
    }
    /**
     * 加入等待
     */
    static function joinWaitQueue($get){
        if( empty($get['uid']) ){
            return httpApiMsg(100,'uid参数不能为空！');
        }
        if( !isset($get['name']) ){
            return httpApiMsg(100,'name参数不存在！');
        }
        if( !isset($get['head_img']) ){
            return httpApiMsg(100,'head_img参数不存在！');
        }
        if( count(Customer::$waitQueues) > 10000 ){
            return httpApiMsg(100,'客服繁忙，请稍后！');
        }
        if( !isset(Customer::$waitQueues[$get['uid']]) ){
            Customer::addWaitQueue($get['uid'],$get['name'],$get['head_img'],0);
            $msg = Message::genWaitCustomerJoin($get['uid'],$get['name'],$get['head_img']);
            foreach( Session::$customerIndex as $access_token=>$v ){
                Session::writeFrameByUid(Session::$sessions[$access_token]['uid'],$msg);
            }
            $welcomeMsg = Message::genCustomerNewUser($get['uid'],'欢迎咨询客服！正在为你接入客服！');
            $sessions = Session::getByUid($get['uid']);
            foreach( $sessions as $session ){
                Session::writeFrameByFd($session['fd'],$welcomeMsg);
            }
        }
        return httpApiMsg(200,'操作成功！');
    }

}